define(function (require) {
    require('rest');
    require('layer');
    require('template');

    return {
        checkIsNull: function (obj) {
            if (typeof (obj) != undefined) {
                if ($(obj).val() == null || $(obj).val() == "") {
                    return false;
                }
                return true;
            }
            return false;
        },
        checkIsNum: function (obj) {
            if (typeof (obj) != undefined) {
                if (isNaN(obj.val())) {
                    return false;
                }
                return true;
            }
            return false;
        },
        checkEmail: function (str) {
            var re = /^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/g;
            return re.test(str);
        },
        checkPhone: function (str) {
            var re = /^1[34578]\d{9}$/g;
            return re.test(str);
        },
        checkPwd: function (str) {
            var re = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,}$/g;
            return re.test(str);
        },
        checkCard: function (str) {
            //15位数身份证正则表达式
            var arg1 = /^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$/;
            //18位数身份证正则表达式
            var arg2 = /^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[a-zA-Z])$/;
            if (str.match(arg1) == null && str.match(arg2) == null) {
                return false;
            } else {
                return true;
            }
        },
        tipsLoading: function (message) {
            return layer.open({
                type: 2,
                content: message
            });
        },
        tipsClose: function (index) {
            if (index) {
                layer.close(index);
            } else {
                layer.closeAll();
            }
        },
        tipsError: function (msg) {
            layer.open({
                content: msg
                , skin: 'msg'
                , time: 2 //2秒后自动关闭
            })
        },
        tipsCallback: function (msg, callback) {
            layer.open({
                content: msg,
                skin: 'msg',
                time: 0.5,
                end: function () {
                    console.log(1)
                    callback();
                }
            })
        },
        tipsSingleSelect: function (msg, btn_text, callback) {
            layer.open({
                className: 'layer-ask',
                content: msg,
                btn: btn_text,
                shadeClose: false,
                yes: function () {
                    callback();
                }
            })
        },
        goTo: function (url) {
            window.location.href = url;
        },
        goBack: function () {
            window.history.go(-1);
        },
        addLocalStorage: function (name, value) {
            if (typeof (localStorage[name]) == "undefined")
                localStorage[name] = value;
            localStorage[name] = value;
        },
        getLocalStorage: function (name) {
            if (typeof (localStorage[name]) == "undefined")
                return null;
            return localStorage[name];
        },
        delLocalStorage: function (name) {
            if (typeof (localStorage[name]) != "undefined")
                localStorage.removeItem(name);
        },
        checkLogin: function () {
            //todo:校验登陆
        },
        getNowPageName: function () {
            var arr = window.location.href.split('.html');
            var arr2 = arr[0].split('/');
            return arr2[arr2.length - 1]
        },
        getLastPageName: function () {
            var arr = document.referrer.split('.html');
            var arr2 = arr[0].split('/');
            return arr2[arr2.length - 1]
        },
        tmpl: function (tmplId, jsonData) {
            return template.render(tmplId, jsonData);
        },
        strFormatReplace: function (text, args) {
            for (var key in args) {
                if (args[key] != undefined) {
                    var reg = new RegExp("({" + key + "})", "g");
                    text = text.replace(reg, args[key]);
                }
            }
            return text;
        },
        showDateFormat: function (date) {
            if (typeof (date) == "object") {
                var month = (date.getMonth() + 1).toString().length != 2 ? 0 + (date.getMonth() + 1).toString() : (date.getMonth() + 1).toString();
                var day = date.getDate().toString().length != 2 ? 0 + date.getDate().toString() : date.getDate().toString();
                return date.getFullYear() + "-" + month + "-" + day;
            }
            return "";
        },
        /**
         * 获取当前页面指定参数
         * @param param 所需获取参数的值
         */
        getUrlParam: function (param) {
            var url = window.location.href;
            if (url.indexOf(param) === -1) {
                return "";
            } else {
                var second = url.split(param + "=")[1];
                if (second.indexOf("&") > -1) {
                    var index = second.indexOf("&");
                    return second.substring(0, index);
                } else {
                    return second.substr(0);
                }
            }
        },
        getLocationHostUrl: function () {
            return window.location.host;
        },
        serializeForm: function (form) {
            var json = {};
            var arrObj = $(form).serializeArray();
            $.each(arrObj, function () {
                if (json[this.name]) {
                    if (!json[this.name].push) {
                        json[this.name] = [json[this.name]];
                    }
                    json[this.name].push(this.value || '');
                } else {
                    json[this.name] = this.value || '';
                }
            });
            return json;
        }
    }
});